package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xca.znb.R;
import com.xca.znb.contact.MainContact;
import com.xca.znb.presenter.MainPresenter;

public class Main extends AppCompatActivity implements View.OnClickListener, MainContact {
    private LinearLayout home , friends , publish , message , mine ;
    private MainPresenter mainPresenter ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainPresenter = new MainPresenter(this);

        home = findViewById(R.id.user_home);
        friends = findViewById(R.id.friends_circle);
        publish = findViewById(R.id.publish);
        message = findViewById(R.id.my_messages);
        mine = findViewById(R.id.mines);


            onHomeButtonClick();
            onFriendsButtonClick();
            onPublishButtonClick();
            onMessageButtonClick();
            onMinesButtonClick();

            replaceFragment(new HomeFragment());
            home.setSelected(true);
            Intent intent = getIntent();
            int num = intent.getIntExtra("toSchoolFragment",0);
            if (num == 100){
                replaceFragment(new SaleSchoolFragment());
                friends.setSelected(true);
                home.setSelected(false);
            }


    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.user_home){
            home.setSelected(true);
            friends.setSelected(false);
            publish.setSelected(false);
            message.setSelected(false);
            mine.setSelected(false);
            replaceFragment(new HomeFragment());
        } else if (v.getId()==R.id.friends_circle) {
            home.setSelected(false);
            friends.setSelected(true);
            publish.setSelected(false);
            message.setSelected(false);
            mine.setSelected(false);
            replaceFragment(new SaleSchoolFragment());
        } else if (v.getId()==R.id.publish) {
            home.setSelected(false);
            friends.setSelected(false);
            publish.setSelected(true);
            message.setSelected(false);
            mine.setSelected(false);
            replaceFragment(new PublishFragment());
        } else if (v.getId()==R.id.my_messages) {
            home.setSelected(false);
            friends.setSelected(false);
            publish.setSelected(false);
            message.setSelected(true);
            mine.setSelected(false);
            replaceFragment(new MessageFragment());
        }else if (v.getId()==R.id.mines){
            home.setSelected(false);
            friends.setSelected(false);
            publish.setSelected(false);
            message.setSelected(false);
            mine.setSelected(true);
            replaceFragment(new MineFragment());
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();//创建fragment管理器
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.Home_Fragment , fragment);//替换对象
        // transaction.addToBackStack(null);//将fragment添加到栈中，达到点击返回移除栈的效果,会造成栈的大量堆叠
        transaction.commit();
    }

    @Override
    public void onHomeButtonClick() {
        home.setOnClickListener(this);
    }

    @Override
    public void onFriendsButtonClick() {
        friends.setOnClickListener(this);
    }

    @Override
    public void onPublishButtonClick() {
        publish.setOnClickListener(this);
    }

    @Override
    public void onMessageButtonClick() {
        message.setOnClickListener(this);
    }

    @Override
    public void onMinesButtonClick() {
        mine.setOnClickListener(this);
    }
}
